package org.kaer.election.adapter;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import org.kaer.election.R;
import org.kaer.election.bean.Candidate;
import org.kaer.election.utils.BitmapStream;
import org.kaer.election.utils.LogUtils;

import java.util.List;

/**
 * Created by lizj on 2016/7/7.
 */
public class CandidateAdapter extends BaseAdapter {
    private Context mContext;
    private List<Candidate> canList;
    private ViewHolder mHolder;
    private Typeface numTf;

    public CandidateAdapter(Context mContext, List<Candidate> canList) {
        this.mContext = mContext;
        this.canList = canList;
        for (Candidate item : canList) {
            LogUtils.i("CandidateAdapter " + item.toString());
        }
        numTf = Typeface.createFromAsset(mContext.getAssets(), "fonts/number.otf");
    }


    @Override
    public int getCount() {
        return canList.size();
    }

    @Override
    public Object getItem(int position) {
        return canList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_candidate_item, null);
            mHolder = new ViewHolder();
            mHolder.ivHead = (ImageView) convertView.findViewById(R.id.layout_candiate_ivHead);
            mHolder.tvName = (TextView) convertView.findViewById(R.id.layout_candiate_tvName);
            mHolder.tvAddr = (TextView) convertView.findViewById(R.id.layout_candiate_tvAddr);
            mHolder.tvCardNum = (TextView) convertView.findViewById(R.id.layout_candiate_tvCardNum);
            mHolder.tvPosition = (TextView) convertView.findViewById(R.id.layout_candidate_tvPosition);
            mHolder.llBg = (FrameLayout) convertView.findViewById(R.id.layout_canItem_llBg);
            mHolder.tvCardNum.setTypeface(numTf);
            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        Candidate info = canList.get(position);
        mHolder.tvName.setText(info.voterName);
        mHolder.tvCardNum.setText(info.voterCertNumber.substring(0, 4) + "********"
                + info.voterCertNumber.substring(info.voterCertNumber.length() - 4));
        mHolder.tvAddr.setText(info.voterAddress);

        if (TextUtils.isEmpty(info.getPositionName())) {
            mHolder.llBg.setBackgroundColor(mContext.getResources().getColor(R.color.white));
            mHolder.tvPosition.setVisibility(View.GONE);
            mHolder.tvPosition.setText(null);

        } else {
            mHolder.llBg.setBackgroundColor(mContext.getResources().getColor(R.color.election_bg));
            mHolder.tvPosition.setVisibility(View.VISIBLE);
            if (TextUtils.isEmpty(mHolder.tvPosition.getText())) {
                //如果setText前该项为空，展示动画效果
                AnimatorSet mSet = new AnimatorSet();
                ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(mHolder.tvPosition, "scaleX", 0f, 1.1f, 1f);
                ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(mHolder.tvPosition, "scaleY", 0f, 1.1f, 1f);
                mSet.playTogether(scaleXAnimator, scaleYAnimator);
                mSet.setInterpolator(new AccelerateDecelerateInterpolator());
                mSet.setDuration(1000);
                mSet.start();

            }
            mHolder.tvPosition.setText(info.getPositionName());

        }
        //照片信息处理
        if (info.voterPicture != null && info.voterPicture.length() > 100) {
            Bitmap bitmap = BitmapStream.stringtoBitmap(info.voterPicture);
            LogUtils.i(bitmap.getWidth() + "///" + bitmap.getHeight());
            mHolder.ivHead.setImageBitmap(BitmapStream.scaleBitmap(bitmap, 73, 77)); // 候选人照片base64编码

        } else {
            if ("女".equals(info.voterGender))
                mHolder.ivHead.setImageResource(R.drawable.person_female);
            else
                mHolder.ivHead.setImageResource(R.drawable.person);

        }
        return convertView;
    }


    public static class ViewHolder {
        private FrameLayout llBg;
        private ImageView ivHead;
        private TextView tvName;
        private TextView tvAddr;
        private TextView tvCardNum;
        private TextView tvPosition;


    }
}
